package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCargoPublishHistoryBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CargoEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.adapter.transport.CargoPublishHistoryAdapter;
import com.sgb.kjwl.viewmodel.transport.CargoPublishHistoryViewModel;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 2020/12/29 17:50
 * @Description: 货源发布选择历史货源页面
 */
public class CargoPublishHistoryActivity extends AppActivity {

    @Inject
    CargoPublishHistoryViewModel viewModel;
    ActivityCargoPublishHistoryBinding binding;
    //竞价模式 0 普通模式  1 竞价模式
    private int mPattern = 0;
    //货源列表
    private List<CargoEntity> mList;
    private CargoPublishHistoryAdapter mAdapter;
    //页码
    private int mPage = 1;

    public static void start(Activity activity, int pattern, int requestCode) {
        Intent intent = new Intent(activity, CargoPublishHistoryActivity.class);
        intent.putExtra(RouterKey.TYPE, pattern);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cargo_publish_history);
        binding.setLifecycleOwner(this);
        binding.includeTitle.titleView.setText("选择货源");
        initRecycler();
        initEvent();
        initData();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new CargoPublishHistoryAdapter(mList);
        binding.rvCpHistory.setLayoutManager(new LinearLayoutManager(getActivity()));
        if (getActivity() != null) {
            DividerDecoration.builder(getActivity())
                    .color(Color.TRANSPARENT)
                    .size(5, TypedValue.COMPLEX_UNIT_DIP)
                    .build()
                    .addTo(binding.rvCpHistory);
        }
        mAdapter.bindToRecyclerView(binding.rvCpHistory);
    }

    private void initEvent() {
        binding.includeTitle.ivBack.setOnClickListener(this);
        binding.srlCpHistory.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getList(CargoPublishHistoryActivity.this, mPage, mPattern);

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPage = 1;
                viewModel.getList(CargoPublishHistoryActivity.this, mPage, mPattern);
            }
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            CargoEntity entity = mList.get(position);
            Intent intent = new Intent();
            intent.putExtra(RouterKey.NO, entity.getNo());
            setResult(RESULT_OK, intent);
            finish();
        });
        //接口回调
        viewModel.ldList.observe(this, this::syncList);
    }

    private void initData() {
        mPattern = getIntent().getIntExtra(RouterKey.TYPE, 0);
        viewModel.getList(this, mPage, mPattern);
    }

    /**
     * 同步列表数据
     */
    @SuppressLint("InflateParams")
    private void syncList(ListResponse<List<CargoEntity>> listResponse) {
        binding.srlCpHistory.finishRefresh();
        binding.srlCpHistory.finishLoadMore();
        if (listResponse == null) {
            return;
        }
        if (mPage == 1) {
            mList.clear();
        }
        if (listResponse.getList() != null) {
            mList.addAll(listResponse.getList());
        }
        //设置空布局
        if (listResponse.getTotal() == 0 && mAdapter.getEmptyViewCount() == 0) {
            View view = LayoutInflater.from(this).inflate(R.layout.layout_logistics_null_list, null, false);
            view.setVisibility(View.VISIBLE);
            mAdapter.setEmptyView(view);
        }
        mAdapter.notifyDataSetChanged();
        //设置页码信息
        if (listResponse.getCurrentPage() < listResponse.getTotal()) {
            mPage = listResponse.getCurrentPage() + 1;
            binding.srlCpHistory.setNoMoreData(false);
        } else {
            binding.srlCpHistory.setNoMoreData(true);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == binding.includeTitle.ivBack) {
            finish();
        }
    }
}
